<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Compare condition | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'condition-compare.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/condition-compare.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/condition-compare.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/condition-compare.html" rel="nofollow" target="_blank">../en/condition-compare.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="xpack-alerting.html">Alerting on cluster and index events</a></span>
»
<span class="breadcrumb-link"><a href="condition.html">Conditions</a></span>
»
<span class="breadcrumb-node">Compare condition</span>
</div>
<div class="navheader">
<span class="prev">
<a href="condition-never.html">« Never condition</a>
</span>
<span class="next">
<a href="condition-array-compare.html">Array compare condition »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="condition-compare"></a>Compare condition<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/condition/compare.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>
<p>Use the <code class="literal">compare</code> condition to perform a simple comparison against a value in
the watch payload. You can use the <code class="literal">compare</code> condition without enabling
dynamic scripting.</p>
<div class="table">
<a id="condition-compare-operators"></a>
<p class="title"><strong>Table 87. Supported comparison operators</strong></p>
<div class="table-contents">
<table border="1" cellpadding="4px" summary="Supported comparison operators">
<colgroup>
<col class="col_1">
<col class="col_2">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">Name</th>
<th align="left" valign="top">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">eq</code></p></td>
<td align="left" valign="top"><p>Returns <code class="literal">true</code> when the resolved value equals the given one (applies
              to numeric, string, list, object and values)</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">not_eq</code></p></td>
<td align="left" valign="top"><p>Returns <code class="literal">true</code> when the resolved value does not equal the given one
              (applies to numeric, string, list, object and null values)</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">gt</code></p></td>
<td align="left" valign="top"><p>Returns <code class="literal">true</code> when the resolved value is greater than the given
              one (applies to numeric and string values)</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">gte</code></p></td>
<td align="left" valign="top"><p>Returns <code class="literal">true</code> when the resolved value is greater/equal than/to the
              given one (applies to numeric and string values)</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">lt</code></p></td>
<td align="left" valign="top"><p>Returns <code class="literal">true</code> when the resolved value is less than the given one
              (applies to numeric and string values)</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">lte</code></p></td>
<td align="left" valign="top"><p>Returns <code class="literal">true</code> when the resolved value is less/equal than/to the
              given one (applies to numeric and string values)</p></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_using_a_compare_condition"></a>Using a compare condition<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/condition/compare.asciidoc">edit</a>
</h3>
</div></div></div>
<p>To use the <code class="literal">compare</code> condition, you specify the value in the execution context
that you want to evaluate, a <a class="xref" href="condition-compare.html#condition-compare-operators" title="Supported comparison operators">comparison operator</a>,
and the value you want to compare against. For example, the following <code class="literal">compare</code>
condition returns <code class="literal">true</code> if the number of the total hits in the
<a class="xref" href="input-search.html" title="Search input">search result</a> is greater than or equal to 5:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "condition" : {
    "compare" : {
      "ctx.payload.hits.total" : { <a id="CO511-3"></a><i class="conum" data-value="1"></i>
        "gte" : 5 <a id="CO511-4"></a><i class="conum" data-value="2"></i>
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO511-1"><i class="conum" data-value="1"></i></a><a href="#CO511-3"></a></p>
</td>
<td align="left" valign="top">
<p>Use dot notation to reference a value in the execution context.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO511-2"><i class="conum" data-value="2"></i></a><a href="#CO511-4"></a></p>
</td>
<td align="left" valign="top">
<p>Specify a comparison operator and the value you want to compare against.</p>
</td>
</tr>
</table>
</div>
<p><a id="compare-condition-date-math"></a>When comparing dates and times, you can use date math expressions
of the form <code class="literal">&lt;{expression}&gt;</code>. For example, the following expression returns
<code class="literal">true</code> if the watch was executed within the last five minutes:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "condition" : {
    "compare" : {
      "ctx.execution_time" : {
        "gte" : "&lt;{now-5m}&gt;"
      }
    }
  }
}</pre>
</div>
<p>You can also compare two values in the execution context by specifying the
compared value as a path of the form of <code class="literal">{{path}}</code>. For example, the following
condition compares the <code class="literal">ctx.payload.aggregations.status.buckets.error.doc_count</code>
to the <code class="literal">ctx.payload.aggregations.handled.buckets.true.doc_count</code>:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "condition" : {
    "compare" : {
      "ctx.payload.aggregations.status.buckets.error.doc_count" : {
        "not_eq" : "{{ctx.payload.aggregations.handled.buckets.true.doc_count}}"
      }
    }
  }
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_accessing_values_in_the_execution_context"></a>Accessing values in the execution context<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/condition/compare.asciidoc">edit</a>
</h3>
</div></div></div>
<p>You use "dot-notation" to access values in the execution context. Values loaded
into the execution context by the input are prefixed by <code class="literal">ctx.payload</code>.</p>
<p>You can reference entries in arrays using their zero-based array indices.
For example, to access the third element of the <code class="literal">ctx.payload.hits.hits</code>
array, use <code class="literal">ctx.payload.hits.hits.2</code>.</p>
<div class="informaltable">
<table border="1" cellpadding="4px">
<colgroup>
<col class="col_1">
<col class="col_2">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">Name</th>
<th align="left" valign="top">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">ctx.watch_id</code></p></td>
<td align="left" valign="top"><p>The id of the watch that is currently executing.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ctx.execution_time</code></p></td>
<td align="left" valign="top"><p>The time execution of this watch started.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ctx.trigger.triggered_time</code></p></td>
<td align="left" valign="top"><p>The time this watch was triggered.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ctx.trigger.scheduled_time</code></p></td>
<td align="left" valign="top"><p>The time this watch was supposed to be triggered.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ctx.metadata.*</code></p></td>
<td align="left" valign="top"><p>Any metadata associated with the watch.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ctx.payload.*</code></p></td>
<td align="left" valign="top"><p>The payload data loaded by the watch’s input.</p></td>
</tr>
</tbody>
</table>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="condition-never.html">« Never condition</a>
</span>
<span class="next">
<a href="condition-array-compare.html">Array compare condition »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>